<?php
/* @file install.php
 * @author Liuexp
 * @description Installing and configuring.
 */
require_once("helper.php");
R::debug(true);
$sql="CREATE TABLE volume (
	name VARCHAR( 200 ) NOT NULL,
	sharetronix_user_id INT( 10 ) UNSIGNED NOT NULL,
	title VARCHAR( 200 ) NOT NULL,
	start_time VARCHAR( 80 ) NOT NULL,
	end_time VARCHAR( 80 ) NOT NULL,
	type ENUM( 'normal', 'contest' ) NOT NULL DEFAULT 'normal',
	visible ENUM( 'on' ) NULL DEFAULT 'on',
	enabled ENUM( 'on' ) NULL DEFAULT 'on',
	PRIMARY KEY ( name)
) ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_general_ci;";
if(!R::$redbean->tableExists('volume')){
	R::exec($sql);
}
else R::exec("DROP TABLE volume;".$sql);


$sql="CREATE TABLE language (
	name VARCHAR( 200 ) NOT NULL,
	title VARCHAR( 200 ) NOT NULL,
	compile_pattern VARCHAR( 200 ) NOT NULL DEFAULT '',
	execute_pattern VARCHAR( 200 ) NOT NULL,
	PRIMARY KEY ( name )
) ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_general_ci;";
if(!R::$redbean->tableExists('language')){
	R::exec($sql);
}
else R::exec("DROP TABLE language;".$sql);


$sql="CREATE TABLE validator (
	name VARCHAR( 200 ) NOT NULL,
	language_name VARCHAR( 200 ) NOT NULL,
	code LONGTEXT NOT NULL,
	PRIMARY KEY ( name )
) ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_general_ci;";
if(!R::$redbean->tableExists('validator')){
	R::exec($sql);
}
else R::exec("DROP TABLE validator;".$sql);


$sql="
CREATE TABLE record (
	run_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
	volume_name VARCHAR( 200 ) NOT NULL,
	problem_name VARCHAR( 200 ) NOT NULL,
	sharetronix_user_id INT( 10 ) UNSIGNED NOT NULL ,
	language_name VARCHAR( 200 ) NOT NULL,
	code LONGTEXT NOT NULL,
	submit_time VARCHAR( 80 ) NOT NULL,
	time INT NOT NULL DEFAULT 0,
	memory INT NOT NULL DEFAULT 0,
	score INT NOT NULL DEFAULT 0,
	message LONGTEXT,
	status ENUM( 'pending', 'waiting', 'running', 'AC', 'WA', 'TLE', 'MLE', 'OLE', 'RE', 'PE', 'CE', 'SE', 'VE' ) NOT NULL DEFAULT 'pending',
	PRIMARY KEY ( run_id )
) ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_general_ci;";
if(!R::$redbean->tableExists('record')){
	R::exec($sql);
}
else R::exec("DROP TABLE record;".$sql);

$sql="CREATE TABLE problem (
		volume_name VARCHAR( 200 ) NOT NULL,
		name VARCHAR( 200 ) NOT NULL,
		sharetronix_user_id INT( 10 ) UNSIGNED NOT NULL ,
		validator_name VARCHAR( 200 ) NOT NULL,
		data LONGTEXT NOT NULL,
		visible ENUM( 'on' ) NULL DEFAULT 'on',
		enabled ENUM( 'on' ) NULL DEFAULT 'on',
		PRIMARY KEY ( volume_name, name )
	) ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_general_ci;";
if(!R::$redbean->tableExists('problem')){
	R::exec($sql);
}
else R::exec("DROP TABLE problem;".$sql);


?>
